package com.example.ssm.controller;


import com.example.ssm.entity.UpComing_Shop;
import com.example.ssm.entity.User;
import com.example.ssm.service.ShopService;
import com.example.ssm.service.UserService;
import com.example.ssm.service.impl.ShopServiceImpl;
import com.example.ssm.service.impl.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/shop")
public class ShopController {
    @Resource
    ShopService shopService;
    @RequestMapping("/ApplyShop")//申请成为商家
    public String addShop(UpComing_Shop shop, Model model) throws IOException {
        UpComing_Shop shop1;
        String a=null;
        shop1=shopService.selectByName(shop);
        System.out.println(shop1);
        if(shop1!=null){
            System.out.println("商户已经被注册");
            model.addAttribute("商户已经被注册",a);
            return "ShopSubmit";
        }else{
            shopService.ToSubmit(shop);
            return "login";
        }
    }

@RequestMapping("/reBack")
    public String reback(String shopName,Model model) throws IOException {
        shopService.del(shopName);
    UserService userService=new UserServiceImpl();
    List<User> list=new ArrayList<User>();
    List<UpComing_Shop> shop=new ArrayList<UpComing_Shop>();
    list =userService.selectAllUser();
    shop =shopService.selectShop();
    model.addAttribute("Shop",shop);
    model.addAttribute("User",list);

        return "admin";
}
    @RequestMapping("/insertmerchant")
    public String insertmerchant(String shopName,Model model) throws IOException {
        UpComing_Shop upComing_shop=new UpComing_Shop();
        UserService userService=new UserServiceImpl();
        upComing_shop.setShop_name(shopName);
        UpComing_Shop shop=shopService.selectByName(upComing_shop);
        User user=new User();
        user.setUname(shop.getUser_name());
        user.setPwd(shop.getPwd());
        user.setPhonenumber(shop.getTel());
        userService.addShop(user);
        ShopService shopService=new ShopServiceImpl();
        shopService.insert(shop);
        shopService.del(shop.getShop_name());
        List<User> list=new ArrayList<User>();
        List<UpComing_Shop> shop1=new ArrayList<UpComing_Shop>();
        list =userService.selectAllUser();
        shop1 =shopService.selectShop();
        model.addAttribute("Shop",shop1);
        model.addAttribute("User",list);
        return "admin";
    }

}
